const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            personal_information: [],
            village: [],
            position: [],
            organization: [],
            category: [],
            government_agencies: [],
            value: '',
            showDialog: false,
            treeData: [],
            tableData: [],
            childDataForm: [],
            isQuery: false,
            Manager: []
        };
    },
    mounted() {
        const volunteer_teams = sessionStorage.getItem('volunteer_teams');
        if (volunteer_teams) {
            const { childDataForm, dataForm } = JSON.parse(volunteer_teams)
            this.childDataForm = childDataForm;
            this.dataForm = dataForm;
            sessionStorage.removeItem('volunteer_teams')
        }
        this.getCategory();
    },
    computed: {
        Agensies_Name() {
            return function (str) {
                let obj = this.government_agencies.filter(i => i.Government_Agencies_SerialNumber == str)[0];
                if (obj) {
                    return obj.Agensies_Name
                }
                return ''
            }
        }
    },
    methods: {
        PH_Validity(rule, value, callback) {
            if (!value) {
                callback(new Error('请输入联系电话'));
            } else if (!validatemobile(value)) {
                callback(new Error('联系电话格式不正确'));
            } else {
                callback();
            }
        },
        changePersonal(val, row) {
            let obj = this.personal_information.filter(i => i.ID_Card_Number == val)[0];
            if (obj) {
                row.Mobile = obj.Contact_Phone
            }
        },
        handleSkip() {
            sessionStorage.setItem('volunteer_teams', JSON.stringify({
                dataForm: this.dataForm,
                childDataForm: this.childDataForm
            }))
            window.location.href = '../../personal_information/personalAdd.html?action=Add&Personal_Resource=3';
        },
        config(arr) {
            arr.forEach(item => {
                if (item.Party_Organization_Type != '支部' && item.Party_Organization_Type != '党委' ) {
                    item.disabled = true;
                }
                item.Government_Agencies_SerialNumber = item.Party_Organization_SerialNumber;
                item.Agensies_Name = item.Party_Organization_Name;
                if (item.children && item.children.length > 0) {
                    this.config(item.children);
                }
            })
        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'government_agencies',
                    NodeCode: ''
                },
                success: (data) => {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                        type: "GET",
                        data: {
                            TableName: 'party_organization',
                            NodeCode: ''
                        },
                        success: (data1) => {
                            this.config(data1)
                            this.treeData = [...data, ...data1];
                            // this.treeData = data; // 直接使用 this
                        },
                        complete: (XMLHttpRequest) => {

                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                    // this.treeData = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'government_agencies',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableName`,
                        type: "POST",
                        data: {
                            Data: 'party_organization',
                            Condition: "Row_Status='0'",
                        },
                        success: (data1) => {
                            this.config(data1);
                            this.government_agencies = [...data, ...data1]; // 直接使用 this
                        },
                        complete: (XMLHttpRequest) => {

                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    })
                    // this.government_agencies = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            })
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'volunteer_teams_members_position',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.position = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    console.log(data);
                    this.personal_information = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'base_system_village',
                    Condition: "TownNumber LIKE '232723%'",
                },
                success: (data) => {
                    this.village = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'party_organization',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.organization = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'volunteer_team_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.category = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/permissionsTree/getGovernment_Agencies_TreeByPermissions`,
                type: "GET",
                data: {
                    Government_AgenciesCode: getCookie('unit'), //请求参数
                },
                success:  (data)=> {
                  this.Manager = data;
                },
                complete: (XMLHttpRequest) => {
                  requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            // $.ajax({
            //     headers: requestHeaders(),
            //     url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
            //     type: "GET",
            //     data: {
            //         TableName: 'government_agencies',
            //         NodeCode: '',
            //     },
            //     success: (data) => {
            //         this.Manager = data; // 直接使用 this
            //     },
            //     complete: (XMLHttpRequest) => {
            //         requestPretreatment(XMLHttpRequest);
            //     },
            //     dataType: "json",
            // });
        },
        handleNodeClick(data) {
            if (!data.disabled) {
                let obj = this.childDataForm.filter(i => i.Responsibility_Unit == data.Government_Agencies_SerialNumber)[0]
                if (!obj) {
                    this.childDataForm.push({
                        Volunteer_Team_SerialNumber: this.dataForm.Volunteer_Team_SerialNumber,
                        Responsibility_Unit: data.Government_Agencies_SerialNumber,
                    })
                } else {
                    obj.Row_Status = '0'
                }
            } 
        },
        remove(item) {
            if (Object.keys(item).some(item => item.includes('_Original'))) {
                let obj = this.childDataForm.filter(i => i.Responsibility_Unit == item.Responsibility_Unit)[0].Row_Status = '1';
            } else {
                this.childDataForm = this.childDataForm.filter(i => i.Responsibility_Unit != item.Responsibility_Unit);
            }
        },
        clear() {
            if (this.action == 'Add') {
                this.childDataForm = [];
            } else {
                this.childDataForm.forEach(item => {
                    if (Object.keys(item).some(item => item.includes('_Original'))) {
                        this.childDataForm.filter(i => i.Responsibility_Unit == item.Responsibility_Unit)[0].Row_Status = '1';
                    } else {
                        this.childDataForm = this.childDataForm.filter(i => i.Responsibility_Unit != item.Responsibility_Unit)
                    }
                });
            }

        },
        query(val) {
            if (val) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'government_agencies',
                        Condition: "Row_Status='0' AND Agensies_Name LIKE '%" + val + "%'",
                    },
                    success: (data) => {
                        this.isQuery = true;
                        this.tableData = data; // 直接使用 this
                    },
                    complete: (XMLHttpRequest) => {

                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                })
            } else {
                this.isQuery = false;
                this.tableData = [];
            }
        }
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader);
app.component('yl-select', ylSelect)
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
